/*================================================================================
*
*
*                                   
*
*
* 项目名称: mcp2515-spi-can-demo
* 文件名称: UI.c
* 创建日期: 21-02-01
* 文件描述: 
*
================================================================================*/
#include "main.h"
#include "RGB_LED.h"
#include "LCD.h"
#include "DBG_PR.h"
#include <stdio.h>

#include "UI.h"

typedef enum tagWidgetState
{
    WIDGET_STAT_NONE,
    WIDGET_STAT_ACTIVE,
}eWidgetStat;

typedef enum tagWidgetItem
{
    WIDGET_BAR_R,
    WIDGET_BAR_G,
    WIDGET_BAR_B,
    WIDGET_BAR_L,
    WIDGET_BAR_NUM,

    WIDGET_NONE,
}eWidgetItem;

typedef struct tagUiPosition
{
    uint16_t u16AnchorX;
    uint16_t u16AnchorY;
    uint16_t u16Width;
    uint16_t u16Height;
}UiPos_t;

typedef struct tagWidgetTypedef
{
    eWidgetStat WidgetStat;
    UiPos_t WidgetPos;
    uint16_t u16Color;
}WidgetTypedef;

//-private code------------------------------------------------------------------
static uint8_t s_u8TouchParamDeal(stType_XPT2046_Coordinate *pCoor);
static void s_WidgetActionDeal(uint8_t u8TouchState, const stType_XPT2046_Coordinate *pCoor);

//-private data------------------------------------------------------------------
static WidgetTypedef s_WidgetArr[WIDGET_BAR_NUM]=
{
    {WIDGET_STAT_NONE, 10,  20, COLOR_BAR_WIDTH, COLOR_BAR_HEIGHT, RED},
    {WIDGET_STAT_NONE, 10,  60, COLOR_BAR_WIDTH, COLOR_BAR_HEIGHT, GREEN},
    {WIDGET_STAT_NONE, 10, 100, COLOR_BAR_WIDTH, COLOR_BAR_HEIGHT, BLUE},
    {WIDGET_STAT_NONE, 10, 140, COLOR_BAR_WIDTH, COLOR_BAR_HEIGHT, WHITE}
};

static char s_StrVal[10]="0";
static ColorBlock_t s_LedSetInfo[WIDGET_BAR_NUM]=
{
    {296,  20, 24, 20, WHITE, s_StrVal},
    {296,  60, 24, 20, WHITE, s_StrVal},
    {296, 100, 24, 20, WHITE, s_StrVal},
    {296, 140, 24, 20, WHITE, s_StrVal},
};

static char tst_str[] = "ZhiNanZhe RGB led DEMO";

static ColorBlock_t Info=
{
    .u16X = 20,
    .u16Y = 180,
    .u16Width = 280,
    .u16Height = 40,
    .u16RGB = WHITE,
    .pStr = tst_str
};

//-public data-------------------------------------------------------------------
stType_XPT2046_Coordinate g_TouchCoor={-1, -1, -1, -1};

/********************************************************************************
*函数功能:
    初始化控件对象显示
*修改记录: 
*日期:                作者:                摘要:
      21-02-01             YummoShin            Create
********************************************************************************/
void InitZhiNanZheUi(void)
{
    uint8_t i;

    ColorBlock_t Background={0, 0, 320, 240, WHITE, NULL};
    LcdDrawColorBlock(&Background);

    for(i=0; i<WIDGET_BAR_NUM; i++)
        LcdDrawColorBar(s_WidgetArr[i].WidgetPos.u16AnchorX, s_WidgetArr[i].WidgetPos.u16AnchorY, 0, s_WidgetArr[i].u16Color);

    for(i=0; i<WIDGET_BAR_NUM; i++)
        LcdDrawColorBlock(&s_LedSetInfo[i]);

    LcdDrawColorBlock(&Info);
}

/********************************************************************************
*函数功能: 
    UI相关处理主函数
*修改记录: 
*日期:                作者:                摘要:
      21-02-01             YummoShin            Create
********************************************************************************/
void ProcessUi(void)
{
    uint8_t u8TouchState=TOUCH_NOT_PRESS;

    u8TouchState = s_u8TouchParamDeal(&g_TouchCoor); //触摸数据处理
    s_WidgetActionDeal(u8TouchState, &g_TouchCoor); //控件屏幕显示处理
}

/********************************************************************************
*函数功能: 
    获取触摸屏坐标,并返回触摸状态
    @param pCoor, point of coordinate
    @retval TouchState
        @arg TOUCH_NOT_PRESS
        @arg TOUCH_PRESSED
*修改记录: 
*日期:                作者:                摘要:
      21-02-02             YummoShin            Create
********************************************************************************/
static uint8_t s_u8TouchParamDeal(stType_XPT2046_Coordinate *pCoor)
{
    uint8_t TouchState=TOUCH_NOT_PRESS;

    if(XPT2046_TouchDetect()==TOUCH_PRESSED)
    {
        XPT2046_Get_TouchedPoint(pCoor, strXPT2046_TouchPara);
        pCoor->pre_x = pCoor->x;
        pCoor->pre_y = pCoor->y;

        TouchState = TOUCH_PRESSED;
    }
    else 
    {
        pCoor->x = pCoor->pre_x = -1;
        pCoor->y = pCoor->pre_y = -1;
    }

    return TouchState;
}

/********************************************************************************
*函数功能: 
    控件触摸动作处理
    @param u8TouchState
    @param pCoor, point
*修改记录: 
*日期:                作者:                摘要:
      21-02-01             YummoShin            Create
********************************************************************************/
static void s_WidgetActionDeal(uint8_t u8TouchState, const stType_XPT2046_Coordinate *pCoor)
{    
    uint8_t i=0, u8Val;
    char Buf[256]={0};
    static eWidgetItem s_ActiveWidget=WIDGET_NONE;
    static RgbLed_t s_Led={0}; //LED控制参数

    //控件触摸动作处理
    switch(u8TouchState)
    {
    case TOUCH_PRESSED:
        if( WIDGET_NONE==s_ActiveWidget )
        {
            //没有控件处于激活状态,判断触摸参数是否落在控件区域
            if( pCoor->x >= s_WidgetArr[WIDGET_BAR_R].WidgetPos.u16AnchorX &&
                pCoor->x < (s_WidgetArr[WIDGET_BAR_R].WidgetPos.u16AnchorX+COLOR_BAR_WIDTH)
            ){
                for(i=0; i<WIDGET_BAR_NUM; i++)
                {
                    if( pCoor->y >= s_WidgetArr[i].WidgetPos.u16AnchorY &&
                        pCoor->y < (s_WidgetArr[i].WidgetPos.u16AnchorY+COLOR_BAR_HEIGHT)
                    ){
                        s_WidgetArr[i].WidgetStat = WIDGET_STAT_ACTIVE;
                        DBG_PR("Widget active\n");
                        s_ActiveWidget = i;
                    }
                }
            }
        }
        else //按下保持
        {
            if(pCoor->x < s_WidgetArr[s_ActiveWidget].WidgetPos.u16AnchorX)
            {
                u8Val = 0;
            }
            else if(pCoor->x > s_WidgetArr[s_ActiveWidget].WidgetPos.u16AnchorX+255)
            {
                u8Val = 255;
            }
            else
            {
                u8Val = pCoor->x-s_WidgetArr[s_ActiveWidget].WidgetPos.u16AnchorX;
            }
            
            LcdDrawColorBar(s_WidgetArr[s_ActiveWidget].WidgetPos.u16AnchorX, 
                            s_WidgetArr[s_ActiveWidget].WidgetPos.u16AnchorY,
                            u8Val,
                            s_WidgetArr[s_ActiveWidget].u16Color);

            if(WIDGET_BAR_R==s_ActiveWidget)
            {
                s_Led.u8R = u8Val;
                sprintf(s_StrVal,"%d", u8Val);
            }
            else if(WIDGET_BAR_G==s_ActiveWidget)
            {
                s_Led.u8G = u8Val;
                sprintf(s_StrVal,"%d", u8Val);
            }
            else if(WIDGET_BAR_B==s_ActiveWidget)
            {
                s_Led.u8B = u8Val;
                sprintf(s_StrVal,"%d", u8Val);
            }
            else
            {
                s_Led.u8L = u8Val/16;
                sprintf(s_StrVal,"%d", u8Val/16);
            }

            LcdDrawColorBlock(&s_LedSetInfo[s_ActiveWidget]);

            DBG_PR("u8Val: %d\nTouchParam: x: %d, y: %d\n", u8Val, pCoor->x, pCoor->y);
        }
        break;

    case TOUCH_NOT_PRESS:
        if(s_ActiveWidget!=WIDGET_NONE) //被控制对象获取参数
        {
            s_WidgetArr[s_ActiveWidget].WidgetStat = WIDGET_STAT_NONE;
            s_ActiveWidget = WIDGET_NONE;

            RgbLedCtrl(&s_Led);

            DBG_PR("There is RGB data set\n");
            DBG_PR("The LED data: %d, %d, %d, %d\n", s_Led.u8R, s_Led.u8G, s_Led.u8B, s_Led.u8L);
        }
        else
        {
            LcdDrawColorBar(s_WidgetArr[WIDGET_BAR_R].WidgetPos.u16AnchorX, s_WidgetArr[WIDGET_BAR_R].WidgetPos.u16AnchorY, g_LedState.u8R, RED);
            LcdDrawColorBar(s_WidgetArr[WIDGET_BAR_G].WidgetPos.u16AnchorX, s_WidgetArr[WIDGET_BAR_G].WidgetPos.u16AnchorY, g_LedState.u8G, GREEN);
            LcdDrawColorBar(s_WidgetArr[WIDGET_BAR_B].WidgetPos.u16AnchorX, s_WidgetArr[WIDGET_BAR_B].WidgetPos.u16AnchorY, g_LedState.u8B, BLUE);
            LcdDrawColorBar(s_WidgetArr[WIDGET_BAR_L].WidgetPos.u16AnchorX, s_WidgetArr[WIDGET_BAR_L].WidgetPos.u16AnchorY, g_LedState.u8L*16, WHITE);

            sprintf(Buf, "LED value R:%d,G:%d,B:%d,L:%d", g_LedState.u8R, g_LedState.u8G, g_LedState.u8B, g_LedState.u8L);
            Info.pStr = Buf;
            LcdDrawColorBlock(&Info);
        }
        break;
    }
}
